from django.shortcuts import render,redirect

# Create your views here.
from dbadmin import models
def user_index(request):
    info = {}
    my_name = request.session.get('username')
    info["my_name"] = my_name
    users = models.users.objects.count()
    projects = models.projects.objects.count()
    salt_server = models.saltconfig.objects.count()
    hosts = models.hosts.objects.count()
    cmd = models.cmdlist.objects.count()
    pre = models.permission_url.objects.count()
    info["users"] = users
    info["projects"] = projects
    info["salt_server"] = salt_server
    info["hosts"] = hosts
    info["cmd"] = cmd
    info["pre"] = pre
    return render(request,"index.html",info)

def user_login(request):
    info = {}
    if request.method == "GET":
        return render(request,"login.html")
    else:
        user_name =  request.POST.get("user_name")
        user_password = request.POST.get("user_password")
        user_status = models.users.objects.filter(user_name=user_name,user_password=user_password).count()
        if request.session.get("user_name"):
            return  redirect("/users/index/")
        else:
            if user_status == 1:
                request.session['is_login'] = 'true'
                request.session['username'] = user_name
                info["my_name"] = user_name
                return  render(request,"welcome.html",info)
            else:
                info["user_error"] = "username or password error"
                return render(request,"login.html")
def user_loginout(request):
    request.session.set_expiry(0)
    request.session.delete("username")
    return redirect("/user/login/")

def user_add(request):
    info = {}
    my_name = request.session.get('username')
    info["my_name"] = my_name
    if request.method == "GET":
        project_list =  models.projects.objects.values()
        permission_list = models.permission_url.objects.values()
        info["url_list"] = permission_list
        info["project_list"] = project_list
        return render(request,"useradd.html",info)
    else:
        user_name = request.POST.get("user_name")
        user_name_cn = request.POST.get("user_name_cn")
        user_password = request.POST.get("user_password")
        project_list = request.POST.getlist("project_list")
        url_list = request.POST.getlist("url_list")
        role = request.POST.get("role")
        user_status = models.users.objects.filter(user_name=user_name).count()
        if user_status == 1:
            pass
        else:
            r=models.users.objects.create(user_name=user_name,user_name_cn=user_name_cn,
                                        user_password=user_password,
                                        user_admin=role)
            # user_id = models.users.objects.filter(user_name=user_name).values("id")[0]["id"]

            obj = models.users.objects.get(id=r.id)
            obj.user_project.set(project_list)
            obj.user_permission.set(url_list)
        return redirect("/user/useradmin/")

def user_admin(request):
    info = {}
    my_name = request.session.get('username')
    info["my_name"] = my_name
    if request.method == "GET":
        user_info_tmp = models.users.objects.values("id","user_name","user_name_cn","user_admin")
        user_list = []
        for user in user_info_tmp:
            user_impower_url = models.users.objects.filter(id=user["id"]).values_list("user_permission__permission_name")
            user_impower_project = models.users.objects.filter(id=user["id"]).values_list("user_project__p_name")
            user_impower_project_tmp = []
            user_impower_url_tmp = []
            for t in user_impower_project:
                user_impower_project_tmp = user_impower_project_tmp + list(t)
            for f in user_impower_url:
                user_impower_url_tmp = user_impower_url_tmp + list(f)
            user["user_url"] = user_impower_url_tmp
            user["project"] = user_impower_project_tmp
            user_list.append(user)


        info["userlist"] = user_list

        return  render(request,"useradmin.html",info)
    else:
        pass

def user_del(request):
    if request.method == "GET":
        id = request.GET.get("id")
        models.users.objects.filter(id=id).delete()
    else:
        pass

    return redirect("/user/useradmin/")

def user_update(request):
    info = {}
    id = request.GET.get("id")
    my_name = request.session.get('username')
    info["my_name"] = my_name
    if request.method == "GET":

        user_info = models.users.objects.filter(id=id).values()[0]
        project_list = models.projects.objects.values()
        url_list = models.permission_url.objects.values()
        info["user_info"] = user_info
        info["project_list"] = project_list
        info["url_list"] = url_list
        return render(request,"userupdate.html",info)
    else:
        user_name = request.POST.get("user_name")
        user_password = request.POST.get("user_password")
        url_list = request.POST.getlist("url_list")
        project_list = request.POST.getlist("project_list")
        user_admin = request.POST.get("role")
        models.users.objects.filter(id=id).update(user_password=user_password,user_admin=user_admin)
        if user_admin == "0":

            obj =  models.users.objects.get(id=id)
            obj.user_project.set(project_list)
            obj.user_permission.set(url_list)
        else:
            pass
        return redirect("/user/useradmin/")




def permission_admin(request):
    info = {}
    my_name = request.session.get('username')
    info["my_name"] = my_name
    if request.method == "GET":
        permission_list = models.permission_url.objects.values()
        info["url_list"] = permission_list
        return  render(request,"urlpermission-list.html",info)
    else:
        permission_name = request.POST.get("permission_name")
        permission_url =  request.POST.get("permission_url")
        permission_status = models.permission_url.objects.filter(permission_url=permission_url).count()
        if permission_status == 1:
            pass
        else:
            models.permission_url.objects.create(permission_name=permission_name,permission_url=permission_url)
        return  redirect("/user/permission_admin/")